package com.yuanda.wordhot.util;

import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

import com.yuanda.wordhot.entity.UserEntity;

/**
 * 
 * @author YuBai
 * @Description  上下文工具类  提供应用上下文的常用方法
 * @Date 2013-4-3
 * @version 1.0
 * @since 1.0
 */
public class ContextUtil implements ApplicationContextAware{
	private static ApplicationContext springFacroty = null;
	private final static Logger logger = LoggerFactory.getLogger(ContextUtil.class);
	
	/**
	 * 取根目录的正确路径，在html引用资源时使用。<br>
	 * 不建议使用此方法，应该使用相对路径，比如<script src="../../js/crm.js"></script><br>
	 * 当然，使用相对路径就需要规范引用路径，即引用bo的路径要与返回的jsp路径相同
	 * @return
	 */
	public static String getRoot() {
		if (root == null) {
			HttpServletRequest request = getRequest();
			root = request.getContextPath() + "/";
		}
		return root;
	}

	public static String root = null;


	/**
	 * 取当前登录的用户。
	 * 注意在用户登录系统时，调用setCurrentUser()方法，将当前用户放到session中
	 * 
	 * @return 当前会话的用户信息
	 */
	public static UserEntity getCurrentUser() {
		HttpSession sess = getHttpSession();
		if (sess != null) {
			return (UserEntity) sess.getAttribute(GlobalConstants.SESSION_ATTR_NAME_CURRENT_USER);
		}
		return null;
	}

	/**
	 * 将当前用户放到session中，在用户登录系统时调用！
	 */
	public static void setCurrentUser(UserEntity userInfo) {
		logger.debug("设置当前登录用户");
		HttpSession sess = getHttpSession();
		if (sess != null) {
			sess.setAttribute(GlobalConstants.SESSION_ATTR_NAME_CURRENT_USER, userInfo);
		}
	}

	/**
	 * 从spring的配置里获得指定的bean
	 * 
	 * @param beanName
	 *          spring配置文件中的bean的id
	 * @return spring为你生成的bean
	 */
	public static Object getBean(String beanName) {
		return springFacroty.getBean(beanName);
	}

	/**
	 * 从spring的配置里获得指定的bean
	 * 
	 * @param beanName
	 * @param clz
	 * @return
	 */
	public static <T> T getBean(String beanName, Class<T> clz) {
		return springFacroty.getBean(beanName, clz);
	}

	/**
	 * 取web上当前用户的会话
	 */
	public static HttpSession getHttpSession() {
		HttpServletRequest httpRequest = getRequest();
		if (httpRequest != null) {
			return httpRequest.getSession();
		} else {
			return null;
		}
	}

	/**
	 * @return 当前请求的http servlet request
	 */
	public static HttpServletRequest getRequest() {
		return (HttpServletRequest) ThreadLocalUtil.get("threadHttpRequest");
	}

	public static HttpServletResponse getResponse() {
		return (HttpServletResponse) ThreadLocalUtil.get("threadHttpResponse");
	}

	/**
	 * 记得在Action或filter里将请求放到每个线程里
	 */
	public static void setRequest(HttpServletRequest request) {
		ThreadLocalUtil.set("threadHttpRequest", request);
	}

	public static void setResponse(ServletResponse response) {
		ThreadLocalUtil.set("threadHttpResponse", response);
	}

	/**
	 * @return 当前请求的参数
	 */
	public static String getRequestParam(String paramName) {
		HttpServletRequest httpRequest = getRequest();
		if (httpRequest != null) {
			String parameter = httpRequest.getParameter(paramName);
			return parameter;
		} else {
			return null;
		}
	}

	/**
	 * 在Controller中将处理后的结果放到request的attr里，以便于jsp页面里可以取到。
	 * 
	 * @param attrName
	 * @param attr
	 */
	public static void setAttrToRequest(String attrName, Object attr) {
		HttpServletRequest httpRequest = getRequest();
		if (httpRequest != null) {
			httpRequest.setAttribute(attrName, attr);
		}
	}
	
	
	/**
	 * 判断当前应用是否为Debug模式
	 * @return
	 */
	public static boolean isDebug(){
		return ConfigUtil.getBoolean(GlobalConstants.SYSTEM_RUNING_MODEL_CONFIG_KEY);
	}

	@Override
	public void setApplicationContext(ApplicationContext context)
			throws BeansException {
		springFacroty = context;
	}
}